﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;
using System.Collections.Generic;

namespace HZ.Supervise.Web.Secondphase
{
    public partial class HospitalScaleListComplit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindArea();
                  banding1();
                  
            }
        }
        /// <summary>
        /// 绑定事件
        /// </summary>
        private void banding1()
        {
            int totalrecord = 0;
            string areaId = "";
            HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
            string strwhere = " ";
            if (!string.IsNullOrEmpty(txtProcureCatalogid.Text))//流水号
            {
                strwhere += " and ProcureCatalogID='" + txtProcureCatalogid.Text + "'";
            }
            if (!txtproductname.Text.Trim().Equals(""))//药品名称
            {
                strwhere += " and ProductName like '%" + txtproductname.Text + "%'";
            }
            if (!txtmedicineMoDelName.Text.Trim().Equals(""))//剂型
            {
                strwhere += " and MedicineModelName like '%" + txtmedicineMoDelName.Text + "%'";
            }
            if (!txtoutlookc.Text.Trim().Equals(""))// 规格
            {
                strwhere += " and Outlookc like '%" + txtoutlookc.Text + "%'";
            }
            if (!txtFactor.Text.Trim().Equals(""))//包装
            {
                strwhere += " and Factor like '%" + txtFactor.Text + "%'";
            }
            if (!string.IsNullOrEmpty(ddlAreaName0.SelectedValue))
            {
                if (ddlAreaName0.SelectedValue == "530101")
                {
                    areaId = "530100";
                }
                else
                {
                    areaId = ddlAreaName0.SelectedValue;
                }
            }
            if (!txtRodi.Text.Trim().Equals(""))
            {
                strwhere += "and  Ratio='" + txtRodi.Text.Trim() + "'";
            }
            strwhere += "and SortSource=" + ddltype.SelectedValue;
            //dt = new DataTable();
            DataTable dt = iprocureCatalog.GetScaleInfo(strwhere, areaId, 0, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
            dt.Columns.Add("Ratio1", typeof(System.String));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Ratio1"] =(Convert.ToDouble(dt.Rows[i]["Ratio"])*100).ToString();
            }
            AspNetPager1.RecordCount = totalrecord;
            gvwDistributionRatio.DataSource = dt;
            gvwDistributionRatio.DataBind();
        }
        /// <summary>
        /// 绑定地区
        /// </summary>
        public void BindArea()
        {
            IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
            IList<Area> lstArea = new List<Area>();
            string userAreaId = CurrentAccount.GetUser().Org.Area.AreaID;
            //判断用户所属地区，如果用户是省级的 则可以列出全部区域
            int i;
            lstArea = ia.GetSonAreas(userAreaId, 1, 0, out i);
            foreach (Area area in lstArea)
            {
                ListItem ddlitem = new ListItem();
                ddlitem.Text = area.AreaName;
                ddlitem.Value = area.AreaID;
                ddlAreaName0.Items.Add(ddlitem);
            }

            ListItem b = new ListItem();
            b.Text = "--请选择--";
            b.Value = "";
            ddlAreaName0.Items.Insert(0, b);
            ddlAreaName0.SelectedIndex = 1;//CurrentAccount.GetUser().Org.Area.AreaID;
            //}

        }
        public string check(object obj) 
        {
            int res =Convert.ToInt32(obj.ToString());
            string back = (res * 100) + "%";
            return back;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            banding1();
        }
        /// <summary>
        /// 全部设置比例
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBatLock_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRatio2.Text))
            {
                HZ.Web.WebUtil.Alert("请填写补助比例！", this);
                return;
            }
            double Ration = Convert.ToDouble(txtRatio2.Text)/100;
            if ((Convert.ToDouble(Ration)) > 1)
            {
                HZ.Web.WebUtil.Alert("补助比例不能大于1！", this);
                return;
            }
            string strwhere = "and SortSource=" + ddltype.SelectedValue;
            HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
            if (RadioAll.Checked)
            {
                DistributionRatio distribution = new DistributionRatio();
                distribution.DistributionName = CurrentAccount.GetUser().UserID;
                string strArea = ddlAreaName0.SelectedValue;
                if (!strArea.Equals(""))
                {
                    if (strArea == "530101")
                    {
                        distribution.DAreaId = "530100";
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                    else
                    {
                        distribution.DAreaId = ddlAreaName0.SelectedValue;
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                }
                else
                {
                    HZ.Web.WebUtil.Alert("请选择地区！", this);
                    return;
                }
                distribution.DistributionState = 0;
                distribution.DistributionTime = DateTime.Now;
                distribution.Ratio = Ration;

                DistributionRatioLog distributionlog = new DistributionRatioLog();
                distributionlog.DistributionName = CurrentAccount.GetUser().UserID;
                distribution.DistributionState = 0;
                if (!strArea.Equals(""))
                {
                    if (strArea == "530101")
                    {
                        distribution.DAreaId = "530100";
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                    else
                    {
                        distribution.DAreaId = ddlAreaName0.SelectedValue;
                        distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                    }
                }
                distributionlog.DistributionTime = DateTime.Now;
                distributionlog.Ratio = Ration;
              int i=  iprocureCatalog.InsertIntoDistributionRatioAll(distribution, distributionlog, strwhere,1);
              if (i > 0)
              {
                  HZ.Web.WebUtil.Alert("设置比例成功！", this);
              }
              else 
              {
                  PageError("设置比例失败","");
              }
                banding1();
            }
            else
            {
                bool checke1 = false;
                IList<DistributionRatio> distributionRationList = new List<DistributionRatio>();
                IList<DistributionRatioLog> distributionRationLogList = new List<DistributionRatioLog>();
                for (int i = 0; i < gvwDistributionRatio.Rows.Count; i++)
                {

                    CheckBox check = (CheckBox)this.gvwDistributionRatio.Rows[i].FindControl("chk");
                    if (check.Checked)
                    {
                        //获取当前的数据判断是否为空，若不为空则更新，若为空则为添加
                        Label lblRation = ((Label)this.gvwDistributionRatio.Rows[i].FindControl("Label1"));
                        string stRation = lblRation.Text;
                        DistributionRatio distribution = new DistributionRatio();
                        distribution.DProcureCatalogid = gvwDistributionRatio.Rows[i].Cells[1].Text;
                        distribution.DistributionName = CurrentAccount.GetUser().UserID;
                        string strArea = ddlAreaName0.SelectedValue;
                        if (!strArea.Equals(""))
                        {
                            if (strArea == "530101")
                            {
                                distribution.DAreaId = "530100";
                                distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                            }
                            else
                            {
                                distribution.DAreaId = ddlAreaName0.SelectedValue;
                                distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                            }
                        }
                        else
                        {
                            HZ.Web.WebUtil.Alert("请选择地区！", this);
                            return;
                        }
                        distribution.DistributionState = 0;

                        distribution.DistributionTime = DateTime.Now;
                        distribution.Ratio = Convert.ToDouble(txtRatio2.Text);
                        Label lblDistributionRatioId = ((Label)this.gvwDistributionRatio.Rows[i].FindControl("lblDistributionRatioId"));
                        distribution.DistributionRatioId = lblDistributionRatioId.Text;
                        //判断补助Id是否为空，若为空则新增，若不为空则更新

                        distributionRationList.Add(distribution);

                        DistributionRatioLog distributionlog = new DistributionRatioLog();
                        distributionlog.DProcureCatalogid = gvwDistributionRatio.Rows[i].Cells[1].Text;
                        distributionlog.DistributionName = CurrentAccount.GetUser().UserID;
                        distribution.DistributionState = 0;
                        if (!strArea.Equals(""))
                        {
                            distributionlog.DAreaId = ddlAreaName0.SelectedValue;
                            distributionlog.DAreaName = ddlAreaName0.SelectedItem.Text;
                        }
                        distributionlog.DistributionRatioId = Guid.NewGuid().ToString();
                        distributionlog.DistributionTime = DateTime.Now;
                        distributionlog.Ratio = Convert.ToDouble(txtRatio2.Text);
                        distributionRationLogList.Add(distributionlog);
                        checke1 = true;
                    }
                }
                if (!checke1)
                {
                    HZ.Web.WebUtil.Alert("请选择要设置比例的数据！", this);
                    return;
                }
                int res = iprocureCatalog.InsertIntoDistributionRatioList(distributionRationList, distributionRationLogList);
                if (res == 1)
                {
                    this.banding1();
                    HZ.Web.WebUtil.Alert("设置比例成功！", this);
                }
                else
                {
                    PageError("批量更新失败！", "");
                }
                RadioOne.Checked = true;
            }
        }

        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtProcureCatalogid.Text = "";
                txtproductname.Text = "";
                txtmedicineMoDelName.Text = "";
                ddlAreaName0.SelectedIndex = -1;
                txtoutlookc.Text = "";
                txtFactor.Text = "";
            }
        }

        protected void gvwDistributionRatio_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.gvwDistributionRatio.EditIndex = -1;
            this.banding1();
        }

        protected void gvwDistributionRatio_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.gvwDistributionRatio.EditIndex = e.NewEditIndex;
            this.banding1();
        }

        protected void gvwDistributionRatio_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                int rowIndex = e.RowIndex;
                string userId = this.gvwDistributionRatio.DataKeys[rowIndex].Value.ToString();
                TextBox txtRation = (TextBox)this.gvwDistributionRatio.Rows[rowIndex].FindControl("txtRatio");
                double  strRation =Convert.ToDouble(txtRation.Text.Trim())/100;
                if (string.IsNullOrEmpty(strRation.ToString()))
                {
                    HZ.Web.WebUtil.Alert("比例不能为空！", this);
                    ((TextBox)gvwDistributionRatio.Rows[rowIndex].FindControl("txtRatio")).Focus();
                    return;
                }
                if (Convert.ToDouble(strRation) > 1)
                {
                    HZ.Web.WebUtil.Alert(" 比例不能大于1！", this);
                    ((TextBox)gvwDistributionRatio.Rows[rowIndex].FindControl("txtRatio")).Focus();
                    return;
                }
                string guid = Guid.NewGuid().ToString();
                HZ.Supervise.IBLL.IBLL_ProcureCatalog iprocureCatalog = HZ.Supervise.BLLFactory.IBLL_ProcureCatalog();
                Label lblRation = ((Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("Label1"));
                string stRation = lblRation.Text;
                DistributionRatio distribution = new DistributionRatio();
                distribution.DProcureCatalogid = gvwDistributionRatio.Rows[rowIndex].Cells[1].Text;
                distribution.DistributionName = CurrentAccount.GetUser().UserID;

                string strArea = ddlAreaName0.SelectedValue;
                if (!strArea.Equals(""))
                {
                    if (ddlAreaName0.SelectedValue.Equals("530101"))
                    {
                        distribution.DAreaId = "530100";
                    }
                    else 
                    {
                        distribution.DAreaId = ddlAreaName0.SelectedValue;
                    }
                   
                    distribution.DAreaName = ddlAreaName0.SelectedItem.Text;
                }
                else
                {
                    Label labAreaId = (Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDAreaId");
                    if (labAreaId.Text.Trim().Equals("530101"))
                    {
                        distribution.DAreaId = "530100";
                    }
                    else
                    {
                        distribution.DAreaId = labAreaId.Text;
                    }
                    distribution.DAreaId = labAreaId.Text;
                    Label labArea = (Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDAreaName");
                    distribution.DAreaName = labArea.Text;
                }
                distribution.DistributionState = 0;
                distribution.DistributionTime = DateTime.Now;
                distribution.Ratio = Convert.ToDouble(strRation);
                DistributionRatioLog distributionlog = new DistributionRatioLog();
                distributionlog.DProcureCatalogid = gvwDistributionRatio.Rows[rowIndex].Cells[1].Text;
                distributionlog.DistributionName = CurrentAccount.GetUser().UserID;
                if (!strArea.Equals(""))
                {
                    if (ddlAreaName0.SelectedValue.Equals("530101"))
                    {
                        distributionlog.DAreaId = "530100";
                    }
                    else
                    {
                        distributionlog.DAreaId = ddlAreaName0.SelectedValue;
                    }
                    //distributionlog.DAreaId = ddlAreaName0.SelectedValue;
                    distributionlog.DAreaName = ddlAreaName0.SelectedItem.Text;
                }
                else
                {
                    Label labAreaId = (Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDAreaId");
                    if (labAreaId.Text.Trim().Equals("530101"))
                    {
                        distributionlog.DAreaId = "530100";
                    }
                    else 
                    {
                        distributionlog.DAreaId = labAreaId.Text;
                    }
                    
                    Label labArea = (Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDAreaName");
                    distributionlog.DAreaName = labArea.Text;
                }
                distributionlog.DistributionState = 0;
                distributionlog.DistributionTime = DateTime.Now;
                distributionlog.Ratio = Convert.ToDouble(strRation);

                string strArea1 = ((Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDAreaId")).Text;
                //若不为空则更新
                int res = 0;
                if (!string.IsNullOrEmpty(stRation))
                {
                    Label lblDistributionRatioId = ((Label)this.gvwDistributionRatio.Rows[rowIndex].FindControl("lblDistributionRatioId"));
                    distribution.DistributionRatioId = lblDistributionRatioId.Text;
                    distributionlog.DistributionRatioId = guid;
                    res = iprocureCatalog.UpdateIntoDistributionRatio(distribution, distributionlog);
                }
                //若为空则添加
                else
                {
                    distributionlog.DistributionRatioId = guid;
                    distribution.DistributionRatioId = guid;
                    res = iprocureCatalog.InsertIntoDistributionRatio(distribution, distributionlog);
                }
                if (res > 0)
                {
                    this.gvwDistributionRatio.EditIndex = -1;
                    this.banding1();
                    HZ.Web.WebUtil.Alert("设置比例成功！", this);
                }
                else
                {
                    PageError("更新失败！", "");
                }
            }
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            banding1();
        }

        protected void gvwDistributionRatio_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
        }
    }
}
